package com.situ.blog.servlet;

import com.situ.blog.pojo.Admin;
import com.situ.blog.pojo.Auth;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.BlogType;
import com.situ.blog.pojo.query.AdminQuery;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IAdminService;
import com.situ.blog.service.IAuthService;
import com.situ.blog.service.impl.AdminserviceImpl;
import com.situ.blog.service.impl.AuthserviceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageRusult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    IAdminService adminService = new AdminserviceImpl();
    IAuthService authService = new AuthserviceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method==null||method.equals("")){
            method = "selectByPage";
        }
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            /*case "selectById":
                selectById(req,resp);
                break;*/
            case "update":
                update(req,resp);
                break;
            case "updateStatus":
                updateStatus(req,resp);
                break;
            case "login":
                login(req,resp);
                break;
            case "logout":
                logout(req,resp);
                break;
        }
    }
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect("/");
    }
    private void login(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.login");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equals(code)){
            Result result = new Result(1,"验证码错误");
            JSONUtil.toJSON(resp, result);
            return;
        }
        Admin admin = adminService.login(name,password);
        System.out.println(admin);
        if (admin != null){
            System.out.println("验证码错误会执行这里吗");
            //HttpSession session = req.getSession();
            session.setAttribute("admin", admin);
            List<Auth> authList = authService.selectByRole(admin.getRole());
            session.setAttribute("authList",authList);
            JSONUtil.toJSON(resp, Result.ok("登录成功"));
            //resp.sendRedirect("/JavaWeb");
        }else {
            JSONUtil.toJSON(resp,Result.error("登陆失败"));
            //req.getRequestDispatcher("/login.jsp").forward(req,resp);

        }
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.deleteAll");
        //传过来的是一个id的数组
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(ids);
        //收到了传过来的id值，然后调用service
        adminService.deleteAll(ids);
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        adminService.updateStatus(id,Integer.parseInt(status));
        JSONUtil.toJSON(resp,Result.ok("更新成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.update");
        Admin admin = MyBeanUtil.copyToBean(req, Admin.class);
        adminService.update(admin);
        Result result = new Result(0,"修改成功");
        JSONUtil.toJSON(resp,result);
    }

    /*private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.selectById");
        String id = req.getParameter("id");
        List<AdminType> list = adminTypeService.selectAll();
        Admin admin = adminService.selectById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("admin",admin);
        System.out.println("servlet中:"+admin+"and list:"+list);
        JSONUtil.toJSON(resp,Result.ok(map));

    }*/

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("AdminServlet.add");
        String name = req.getParameter("name");
        System.out.println(name);
        //封装一下

        Admin admin = MyBeanUtil.copyToBean(req, Admin.class);
        System.out.println(admin);
        adminService.add(admin);
        //返回Ajax数据
        Result result = new Result(0,"添加成功");
        JSONUtil.toJSON(resp,result);
    }


    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("AdminServlet.deleteById");
        //接收参数
        String id = req.getParameter("id");
        System.out.println("id:"+id);
        //调用函数
        int count = adminService.deleteById(Integer.parseInt(id));//没有返回值
        Result result = null;
        if (count>0){
            result = new Result(0,"删除成功");
        }else {
            result = new Result(1,"删除失败");
        }
        //固定的三行
        JSONUtil.toJSON(resp,result);
        //*resp.setContentType("text/html;charset=UTF-8");
        //ObjectMapper mapper = new ObjectMapper();
        //mapper.writeValue(resp.getWriter(), result);//*//现在不知道这里要返回什么
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("AdminServlet.selectByPage");
        /*String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");*/
        //1.封装所需数据page:当前页码 ;limit:每页的信息数 ;name:查找的课程名字 ;credit:查找的学分

        //调用位于service层的分页查找功能
        AdminQuery adminQuery = MyBeanUtil.copyToBean(req, AdminQuery.class);
        System.out.println(adminQuery);
        PageRusult pageResult = adminService.selectByPage(adminQuery);

        JSONUtil.toJSON(resp, pageResult);

    }
}
